﻿#include "timeoutdialog.h"
#include "ui_timeoutdialog.h"
#include <QTimer>

static const int BREAKSTEPS = 3;

TimeoutDialog::TimeoutDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TimeoutDialog)
{
    ui->setupUi(this);
    ui->progressBar->setMaximum(breakSteps);
    ui->label->setText(QString::asprintf("%d/%d", curStep, breakSteps));

    timer=new QTimer();
    connect(timer, &QTimer::timeout, this, &TimeoutDialog::updateOneSec);

    breakSteps=BREAKSTEPS;

    this->setWindowFlags(this->windowFlags() &~Qt::WindowCloseButtonHint);
}

TimeoutDialog::~TimeoutDialog()
{
    delete ui;
    delete timer;
}

void TimeoutDialog::setPostoff(bool enable)
{
    ui->pushButton->setEnabled(enable);
}

void TimeoutDialog::start()
{
    timer->start(1000);
}
void TimeoutDialog::updateOneSec()
{
    curStep+=1;
    if(curStep<breakSteps)
    {
        ui->progressBar->setValue(curStep);
        ui->label->setText(QString::asprintf("%d/%d", curStep, breakSteps));
    }
    else
    {
        emit sendNormalBreak();
    }
}

void TimeoutDialog::on_pushButton_clicked()
{
    emit sendPostoffBreak();
}

